package com.ihelp.rest.mis.platform.service;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ihelp.rest.mis.platform.dao.OnlinetimeDao;
import com.ihelp.rest.mis.pojo.Onlinetime;
import com.ihelp.rest.utils.JsonCommon;

@Service
public class OnlinetimeService {
	@Autowired
	private OnlinetimeDao onlinetimeDao;
	
	/**
	 * 查询推荐年龄上网时长  返回JSON串
	 * @param queryCondition
	 * @param start
	 * @param pageSize
	 * @param sort
	 * @param order
	 * @return
	 */
	public String findOnlinetimePage(Onlinetime queryCondition,int start,int pageSize,String sort, String order){
		return JsonCommon.getGridJson(this.onlinetimeDao.queryOnlinetimePage(queryCondition,start,pageSize,sort,order)).toString();
	}
	
	public Onlinetime getOnlinetimeById(String id){
		return this.onlinetimeDao.get(id);
	}
	
	/**
	 * 保存推荐上网时长
	 * @param onlinetime
	 * @return
	 */
	public String saveOnlinetime(Onlinetime onlinetime){
		Map<String ,Object> result = new HashMap<String, Object>();
		result.put("success", false);
		try{
			if(StringUtils.isBlank(onlinetime.getId()))
				onlinetime.setId(null);
			long l_count = this.onlinetimeDao.validAge(onlinetime);
			if(l_count>0){
				result.put("data", "此年龄推荐上网时长已存在！");
			}else{
				this.onlinetimeDao.saveOrUpdate(onlinetime);
				result.put("success", true);
				result.put("data", "保存成功");
			}	
		}catch(Exception e){
			result.put("data", "保存失败"+e.getMessage());
		}
		return JSONObject.fromObject(result).toString() ;
	}
	
	/**
	 * 删除推荐上网时长
	 * @param ids
	 * @return
	 */
	public String delOnlinetime(String ids){
		Map<String ,Object> result = new HashMap<String, Object>();
		result.put("success", false);
		try{
			this.onlinetimeDao.dropOnline(ids);
			result.put("success", true);
			result.put("data", "删除成功");
		}catch(Exception e){
			result.put("data", "删除失败"+e.getMessage());
		}
		return JSONObject.fromObject(result).toString() ;
	}
}
